/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.struts.service;

import opiam.admin.faare.struts.service.beans.JBMenus;

import org.apache.commons.digester.Digester;

import org.apache.log4j.Logger;

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import java.io.IOException;
import java.io.InputStream;


/**
 * Menus sub-service configuration file loading from the XML file.
 *
 * The XML file must contain the following lines :
 * <code><br>
 * &lt;!DOCTYPE mapping PUBLIC<br>
 *         "-//OPIAM ADMIN//DTD Menus Configuration 1.0//EN"<br>
 *         "http://www.opiam.org/admin/dtd/menus_conf.dtd"&gt;<br>
 * </code>
 *<br>
 * The second line is registered to the digester (it does not need to be an actual URL).
 *
 */
public final class MenusConfig implements ErrorHandler
{
    /** Log4J. */
    private static Logger _logger = Logger.getLogger(MenusConfig.class.getName());

    /** DOCTYPE key. */
    public static final String MENU_CONFIG_DTD_KEY = "-//OPIAM ADMIN//DTD Struts Menu Configuration 1.0//EN";

    /** DOCTYPE URL. */
    public static final String MENU_CONFIG_DTD_URL = "/config/service/struts/menus_conf.dtd";

    /** XML Parser. */
    private static Digester _digester = null;

    /** Current instance. */
    private static MenusConfig _instance = null;

    /** Parser exception. */
    private static SAXParseException _saxParseException = null;

    /**
     * Constructor for ProfilesDigester.
     */
    private MenusConfig()
    {
        super();
    }

    /**
     * Digester initialization with DTD fields.
     */
    private static void initProfileDigester()
    {
        _saxParseException = null;
        _digester = new Digester();

        _digester.setErrorHandler(_instance);
        _digester.setValidating(true);
        _digester.register(MENU_CONFIG_DTD_KEY,
            OperatorsConfig.class.getResource(MENU_CONFIG_DTD_URL).toString());

        _digester.addObjectCreate("menus",
            "opiam.admin.faare.struts.service.beans.JBMenus");

        _digester.addObjectCreate("menus/feature",
            "opiam.admin.faare.struts.service.beans.JBFeature");
        _digester.addSetNext("menus/feature", "addFeature",
            "opiam.admin.faare.struts.service.beans.JBFeature");
        _digester.addSetProperties("menus/feature");

        _digester.addObjectCreate("menus/profile",
            "opiam.admin.faare.struts.service.beans.JBProfileMenu");
        _digester.addSetNext("menus/profile", "addProfileMenu",
            "opiam.admin.faare.struts.service.beans.JBProfileMenu");
        _digester.addSetProperties("menus/profile");

        _digester.addObjectCreate("menus/profile/menu",
            "opiam.admin.faare.struts.service.beans.JBMenu");
        _digester.addSetNext("menus/profile/menu", "addMenu",
            "opiam.admin.faare.struts.service.beans.JBMenu");
        _digester.addSetProperties("menus/profile/menu");

        _digester.addObjectCreate("menus/profile/menu/element",
            "opiam.admin.faare.struts.service.beans.JBMenuElement");
        _digester.addSetNext("menus/profile/menu/element", "addElement",
            "opiam.admin.faare.struts.service.beans.JBMenuElement");
        _digester.addSetProperties("menus/profile/menu/element");
    }

    /**
     * Gets MenusConfig instance.
     *
     * @return MenusConfig instance
     */
    public static MenusConfig getInstance()
    {
        if (_instance == null)
        {
            _instance = new MenusConfig();
            initProfileDigester();
        }

        return _instance;
    }

    /**
     * Gets menus config.
     *
     * @param in configuration stream
     *
     * @return Menus config
     *
     * @throws IOException exception while reading stream.
     * @throws SAXException exception while parsing data.
     */
    public JBMenus readConfig(InputStream in) throws IOException, SAXException
    {
        JBMenus result = null;
        result = (JBMenus) _digester.parse(in);

        if (_saxParseException != null)
        {
            _logger.debug("Exception : " + _saxParseException.getMessage());
            throw new SAXException(_saxParseException.getMessage());
        }

        result.compute();

        return result;
    }

    /**
     * Parsing error callback.
     *
     * @param saxParseException parsing exception
     */
    @Override
	public void error(SAXParseException saxParseException)
    {
        _logger.debug(" error exception!!!!!!!!!!!!!!!");
        _saxParseException = saxParseException;
    }

    /**
     * Parsing fatal error callback.
     *
     * @param saxParseException parsing exception
     */
    @Override
	public void fatalError(SAXParseException saxParseException)
    {
        _logger.debug("fatalError exception!!!!!!!!!!!!!!!");
        _saxParseException = saxParseException;
    }

    /**
     * Parsing warning callback.
     *
     * @param saxParseException parsing exception
     */
    @Override
	public void warning(SAXParseException saxParseException)
    {
        _logger.debug("warning : " + saxParseException.getMessage());
    }
}
